package com.moter.one.wyydemo1.backstage.controller;

import com.moter.one.wyydemo1.entity.SongInfo;
import com.moter.one.wyydemo1.entity.UserInfo;
import com.moter.one.wyydemo1.mapper.SongInfoMapper;
import com.moter.one.wyydemo1.service.SongInfoService;
import com.moter.one.wyydemo1.backstage.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class AxiosSongController {
    @Autowired
    SongInfoService songInfoService;

    @PostMapping("admin/addsong")
    public void add(@RequestBody SongInfo songInfo){
        songInfoService.insertSong(songInfo);
    }

    @PostMapping("/admin/song/queryAllByPage")
    public DataVo<List<SongInfo>> getAll(Integer page,Integer limit){//这里是layui自动传递的，我们拿就行。
        List<SongInfo> songInfos = songInfoService.queryPageInfo((page-1)*limit,limit);
        //因为layui分页要返回的json是四个字段所以建了这个泛型类：具体可以去看看这个类的注释： 最后一个参数count 是数据库里面所有数据的个数，所以我在这里重新了一次所有数据然后得到.size()
        DataVo<List<SongInfo>> dataVo = new DataVo<>(0, "查询成功", songInfos,songInfoService.queryAll().size());
        //code是状态 为0就行，msg是信息，中间的就是返回的数据，最后一个count 是数据库的所有信息
        return dataVo;
    }
    @PostMapping("/admin/searchsong")
    public DataVo<List<SongInfo>> search(String lyrics,String song_name,String singer_name){


        List<SongInfo> songInfos = songInfoService.searchBy_id_name_singer(lyrics, song_name, singer_name);
        DataVo<List<SongInfo>> dataVo = new DataVo<>(0, "查询成功", songInfos,songInfos.size());
        System.out.println(dataVo);
        return dataVo;
    }
    @PostMapping("/admin/deletesong")
    public Integer delete(@RequestBody SongInfo songInfo){
        System.out.println("songInfo = " + songInfo);
        Integer res=songInfoService.deleteById(songInfo.getSong_id());
        System.out.println("res = " + res);
        return res;
    }
    @ResponseBody
    @CrossOrigin
    @RequestMapping("searchByItem")
    public List<SongInfo> searchByItem(String item){
        System.out.println("item = " + item);
        return songInfoService.searchByItem(item);
    }
}
